﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using AliPayApi.Core;

namespace AliPayApi.Api.Batch_Refund
{
    public class SendRefund
    {
        #region 支付属性

        //服务器异步通知页面路径 
        private string platform_Notify_Url;
        /// <summary>
        /// 服务器异步通知页面路径，需http://格式的完整路径，不能加?id=123这类自定义参数
        /// </summary>
        public string Platform_Notify_Url
        {
            get { return platform_Notify_Url; }
            set { platform_Notify_Url = value; }
        }

        //本支付接口异步通知页面路径
        private string _notify_url;
        /// <summary>
        /// 本支付接口异步通知页面路径，需http://格式的完整路径，不能加?id=123这类自定义参数
        /// </summary>
        public string Notify_url
        {
            get { return _notify_url; }
            set { _notify_url = value; }
        }

        //卖家支付宝帐户
        private string _seller_email;
        /// <summary>
        /// 卖家支付宝帐户,必填
        /// </summary>
        public string Seller_email
        {
            get { return _seller_email; }
            set { _seller_email = value; }
        }

        //退款当天日期
        private string _refund_date;
        /// <summary>
        /// 退款当天日期，必填，格式：年[4位]-月[2位]-日[2位] 小时[2位 24小时制]:分[2位]:秒[2位]，如：2007-10-01 13:13:13
        /// </summary>
        public string Refund_date
        {
            get { return _refund_date; }
            set { _refund_date = value; }
        }

        //批次号
        private string _batch_no;
        /// <summary>
        /// 批次号，必填，格式：当天日期[8位]+序列号[3至24位]，如：201008010000001
        /// </summary>
        public string Batch_no
        {
            get { return _batch_no; }
            set { _batch_no = value; }
        }

        //退款笔数
        private string _batch_num;
        /// <summary>
        ///退款笔数， 必填，参数detail_data的值中，“#”字符出现的数量加1，最大支持1000笔（即“#”字符出现的数量999个）
        /// </summary>
        public string Batch_num
        {
            get { return _batch_num; }
            set { _batch_num = value; }
        }

        //退款详细数据
        private string _detail_data;
        /// <summary>
        /// 退款详细数据，必填，具体格式请参见接口技术文档
        /// </summary>
        public string Detail_data
        {
            get { return _detail_data; }
            set { _detail_data = value; }
        }

        //返回数据的编码格式
        private string _responeCode;
        /// <summary>
        /// 返回数据的编码格式
        /// </summary>
        public string ResponeCode
        {
            get { return _responeCode; }
            set { _responeCode = value; }
        }

        #endregion

        public SendRefund()
        {
            Platform_Notify_Url = Config.GetAppSetting("platform_notify_url");
            Notify_url = Config.GetAppSetting("notify_url");
            Seller_email = "";
            Refund_date = "";
            Batch_no = "";
            Batch_num = "";
            Detail_data = "";
            ResponeCode = "utf-8";
        }

        private bool CheckParamsBeforeSend()
        {
            var checkList = new List<string>();
            var checkListName = new List<string>();

            checkList.Add(Platform_Notify_Url);
            checkListName.Add("Platform_Notify_Url");

            checkList.Add(Notify_url);
            checkListName.Add("Notify_url");

            checkList.Add(Seller_email);
            checkListName.Add("Seller_email");

            checkList.Add(Refund_date);
            checkListName.Add("Refund_date");

            checkList.Add(Batch_no);
            checkListName.Add("Batch_no");

            checkList.Add(Batch_num);
            checkListName.Add("Batch_num");

            checkList.Add(Detail_data);
            checkListName.Add("Detail_data");

            var allowChek = true;
            var index = 0;
            foreach (var tmp in checkList)
            {
                if (string.IsNullOrEmpty(tmp))
                {
                    allowChek = false;
                    throw new Exception(string.Format("{0}为必填项", checkListName[index]));
                }
                index += 1;
            }
            return allowChek;
        }

        /// <summary>
        /// form提交到对应网址
        /// </summary>
        /// <returns></returns>
        public string SendForm()
        {
            if (!CheckParamsBeforeSend())
            {
                return null;
            }

            //把请求参数打包成数组
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("partner", Config.Partner);
            sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
            sParaTemp.Add("service", "refund_fastpay_by_platform_pwd");
            sParaTemp.Add("notify_url", Platform_Notify_Url);
            sParaTemp.Add("seller_email", Seller_email);
            sParaTemp.Add("refund_date", Refund_date);
            sParaTemp.Add("batch_no", Batch_no);
            sParaTemp.Add("batch_num", Batch_num);
            sParaTemp.Add("detail_data", Detail_data);

            //建立请求
            return Submit.BuildRequest(sParaTemp, "get", "提交");
        }

        /// <summary>
        /// 模拟Post提交，不改变当前网址
        /// </summary>
        /// <returns></returns>
        public string SendBySimulationPost()
        {
            if (!CheckParamsBeforeSend())
            {
                return null;
            }

            //把请求参数打包成数组
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("partner", Config.Partner);
            sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
            sParaTemp.Add("service", "refund_fastpay_by_platform_pwd");
            sParaTemp.Add("notify_url", Platform_Notify_Url);
            sParaTemp.Add("seller_email", Seller_email);
            sParaTemp.Add("refund_date", Refund_date);
            sParaTemp.Add("batch_no", Batch_no);
            sParaTemp.Add("batch_num", Batch_num);
            sParaTemp.Add("detail_data", Detail_data);

            //建立请求
            return Submit.BuildRequest(sParaTemp, ResponeCode);
        }
    }
}
